package hw.ats.apps.APP_ID;


import hw.ats.apps.AbstractStartupAct;
//import hw.ats.news.R;
import hw.ats.apps.APP_ID.R;
import hw.ats.news.util.App;
import android.os.Bundle;
import android.app.Activity;
import android.os.IBinder;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.content.Intent;
import com.skt.arm.aidl.*;


/**
 * APP_ID
 * @author pcraft 
 */
public class StartupAct extends AbstractStartupAct {
	@SuppressWarnings("unused")
	private final static String TAG = "StartupAct";
	private IArmService service;
	private ArmServiceConnection armCon;
	private String AID  = "APP_AID";
	
	/**
	 * AD XML URL
	 */
	private final static String AD_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/ads";
	
	/**
	 * 환경설정 XML URL
	 */
	private final static String CONFIG_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/config";
	
	/**
	 * 뉴스 XML URL
	 */
	private final static String NEWS_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/entries?page=";
	
	/**
	 * 추천글 XML URL
	 */
	private final static String NEWS_RECMD_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/entries?starred=1";
	
	/**
	 * 추천글 XML URL
	 */
	private final static String NEWS_BODY_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/entry/";
	
	/**
	 * 카테고리 소속 글 XML URL
	 */
	private final static String NEWS_CATE_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/entries?mode=audio&category_id=";
	
	/**
	 * 카테고리 목록 XML URL
	 */
	private final static String CATEGORY_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/categories";

    /**
    * 동영상 목록 XML URL
    */
    private final static String NEWS_MOV_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/entries_with_moving_picture?page=";

	/**
	 * 댓글 XML URL
	 */
	private final static String CMT_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/comments/";
	
	/**
	 * 댓글 등록 URL
	 */
	private final static String CMT_ADD_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/comment/create";
	
	/**
	 * 댓글 수정 URL
	 */
	private final static String CMT_MOD_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/comment/update/";
	
	/**
	 * 댓글 삭제 URL
	 */
	private final static String CMT_DEL_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/comment/delete/";

    /**
     * 댓글 카운트 URL
     */
     private final static String CMT_CNT_XML_URL = "http://api.allthatfactory.com/APP_ID/openapi/live/comment_count/";
	
	private final static int RES_LOADING_110x75 = R.drawable.ic_loading_newsapp_110x75;
	private final static int RES_LOADING_225x165 = R.drawable.ic_loading_newsapp_225x165;
	private final static int RES_LOADING_480x265 = R.drawable.ic_loading_newsapp_480x265;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        runArmService();
    }
    
    private boolean runArmService(){
    	try{
    		if (armCon == null){
    			armCon = new ArmServiceConnection();
    			bindService(new Intent(IArmService.class.getName()),
    					armCon, Context.BIND_AUTO_CREATE);
    		}
    		return true;
    	}catch(Exception e ){
    		releaseService();
    		return false;
    	}
    }
    private void releaseService(){
    	if (armCon != null){
    		unbindService(armCon);
    		armCon = null;
    	}
    }
    class ArmServiceConnection implements ServiceConnection {
    	public void onServiceConnected(ComponentName name, IBinder boundService){
    	  if (service == null)
    		  service = IArmService.Stub.asInterface((IBinder)boundService);
    	
    	  try{
    		  int res = service.executeArm(AID);
    		  switch(res){
    		  case 1:
    		        setContentView(R.layout.act_splash);
    		        // 상수 초기화
    		        App.PACKAGE = getApplicationContext().getPackageName();
    		        App.CONFIG_XML_URL = CONFIG_XML_URL;
    		        App.CATEGORY_XML_URL = CATEGORY_XML_URL;
    		        App.NEWS_XML_URL = NEWS_XML_URL;
    		        App.CMT_XML_URL = CMT_XML_URL;
    		        App.CMT_ADD_XML_URL = CMT_ADD_XML_URL;
    		        App.CMT_MOD_XML_URL = CMT_MOD_XML_URL;
    		        App.CMT_DEL_XML_URL = CMT_DEL_XML_URL;
    		        App.NEWS_RECMD_XML_URL = NEWS_RECMD_XML_URL;
    		        App.NEWS_BODY_XML_URL = NEWS_BODY_XML_URL;
    		        App.NEWS_CATE_XML_URL = NEWS_CATE_XML_URL;
                    App.NEWS_MOV_XML_URL  = NEWS_MOV_XML_URL;
    		        App.AD_XML_URL = AD_XML_URL;
                    App.CMT_CNT_XML_URL = CMT_CNT_XML_URL;
    		        App.RES_LOADING_110x75 = RES_LOADING_110x75;
    		        App.RES_LOADING_225x165 = RES_LOADING_225x165;
    		        App.RES_LOADING_480x265 = RES_LOADING_480x265;
    		        init();
    			  break;
    		  default:
    			  finish();
    			  onDestroy();
    			  break;
    		  }
    	  }catch(Exception e ){
    		  releaseService();
    		  return;
    	  }
    	  releaseService();
    	  }
    	public void onServiceDisconnected(ComponentName name){
    		service = null;
    	}
    }
    
}
